package com.fan.analysis.module.analysis.warning.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fan.analysis.common.constant.ResponseCodeConst;
import com.fan.analysis.common.domain.ResponseDTO;
import com.fan.analysis.module.analysis.group.dao.UserRelationDao;
import com.fan.analysis.module.analysis.group.domain.entity.UserInfoEntity;
import com.fan.analysis.module.analysis.group.domain.entity.UserRelationEntity;
import com.fan.analysis.module.analysis.warning.dao.UserWarningDao;
import com.fan.analysis.module.analysis.warning.domain.vo.DefaultUserVo;
import com.fan.analysis.module.analysis.warning.domain.vo.SensitiveUserVo;
import com.fan.analysis.module.analysis.warning.service.UserWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author fan
 * @date 2021/10/08 16:44
 **/
@Service
public class UserWarningServiceImpl implements UserWarningService {

    private final UserWarningDao userWarningDao;

    private final UserRelationDao userRelationDao;

    @Autowired
    public UserWarningServiceImpl(UserWarningDao userWarningDao, UserRelationDao userRelationDao) {
        this.userWarningDao = userWarningDao;
        this.userRelationDao = userRelationDao;
    }

    /**
     * 直接返回所有负面用户和一个默认的用户
     *
     * @return 所有负面用户和一个默认的用户
     */
    @Override
    public ResponseDTO<SensitiveUserVo> getSensitiveUser() {

        SensitiveUserVo sensitiveUserVo = new SensitiveUserVo();
        List<UserInfoEntity> userInfoEntityList = userWarningDao.selectList(new QueryWrapper<UserInfoEntity>().eq("is_sentive_user", "Y"));
        for (UserInfoEntity userInfoEntity : userInfoEntityList) {
            BigDecimal bg = BigDecimal.valueOf(userInfoEntity.getInclination());
            userInfoEntity.setInclination((float) bg.setScale(2, RoundingMode.HALF_UP).doubleValue());
        }
        sensitiveUserVo.setData(userInfoEntityList);
        if (userInfoEntityList.size() > 0) {
            // 设置默认的用户
            UserInfoEntity userInfoEntity = userInfoEntityList.get(0);

            DefaultUserVo defaultUserVo = new DefaultUserVo();
            defaultUserVo.setInfo(userInfoEntity);

            //获取常联系人
            List<UserRelationEntity> userRelationEntities = userRelationDao.listFrequentContacts(userInfoEntity.getPhoneNum(), 5);
            defaultUserVo.setFrequentContacts(userRelationEntities);

            sensitiveUserVo.setDefaultUser(defaultUserVo);
        }

        return ResponseDTO.succData(sensitiveUserVo);
    }

    /**
     * 根据手机号搜索自己信息和常用联系人信息
     *
     * @return 返回
     */
    @Override
    public ResponseDTO<DefaultUserVo> getThisPhoneNumInfo(String phoneNum) {
        UserInfoEntity userInfoEntity = userWarningDao.selectOne(new QueryWrapper<UserInfoEntity>().eq("phone_num", phoneNum));
        if (userInfoEntity == null) {
            return ResponseDTO.wrap(ResponseCodeConst.NOT_EXISTS);
        }
        DefaultUserVo defaultUserVo = new DefaultUserVo();
        defaultUserVo.setInfo(userInfoEntity);
        //获取常联系人
        List<UserRelationEntity> userRelationEntities = userRelationDao.listFrequentContacts(userInfoEntity.getPhoneNum(), 5);
        defaultUserVo.setFrequentContacts(userRelationEntities);

        return ResponseDTO.succData(defaultUserVo);
    }
}
